package gui.programchair;
 
import gui.CAFrame;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.Set;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import caobject.Conference;
import caobject.EnumRole;
import caobject.Manuscript;
import caobject.User;
 
/** 
 * @author Tom Netzer
 * @version December 3, 2011
 * This is the class that holds the center panel of the frame
 * when the user is a program chair and displays a table in
 * the frame with the manuscripts and their statuses.
 */
public class pcCenterPanel extends JPanel {
    private final Conference my_conf;
    private final Manuscript[] my_mans;
    private final CAFrame my_frame;
    private User my_user = new User("PC", EnumRole.PROGRAMCHAIR);
    private JTable my_table;
    private TableModel my_model;
    
    public pcCenterPanel(CAFrame the_frame, Conference the_conf) {
        super(new GridLayout(1,0));
    	
        my_frame = the_frame;
        my_conf = the_conf;
        my_mans = my_conf.getManuscripts().toArray(new Manuscript[0]);
		my_model = new MyTableModel();    	 
        my_table = new JTable(my_model);
        my_table.setPreferredScrollableViewportSize(new Dimension(1000, 600));
        my_table.setFillsViewportHeight(true);
        
        my_model.addTableModelListener(new TableModelListener() {

			@Override
			public void tableChanged(final TableModelEvent tme) {
				int row = tme.getFirstRow();
				int col = tme.getColumn();
				String sel = (String) my_model.getValueAt(row, col);
				
				String author = (String) my_model.getValueAt(row, 0); 
				String title = (String) my_model.getValueAt(row, 1);
				for (Manuscript man : my_mans) {
					if (man.getTitle().equalsIgnoreCase(title) &&
							man.getAuthor().equalsIgnoreCase(author)) {
						man.setStatus(sel, my_user);
						break;
					}
				}
			}
        	
        });
        
        //----------For drop down menu to change status----
        setUpStatusColumn(my_table.getColumnModel().getColumn(3));
        
        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(my_table);
 
        //Add the scroll pane to this panel.
        add(scrollPane);
        
    }
 
    //adds drop down menu to change status fields
    private void setUpStatusColumn(TableColumn statusColumn) {
		JComboBox my_box = new JComboBox();
		my_box.addItem("YES");
		my_box.addItem("NO");
		my_box.addItem("UNDECIDED");
		
		statusColumn.setCellEditor(new DefaultCellEditor(my_box));
		
		//adds a message if the user hovers the mouse over
		//the status cells.
        DefaultTableCellRenderer renderer =
                new DefaultTableCellRenderer();
        renderer.setToolTipText("Click for combo box");
        statusColumn.setCellRenderer(renderer);
	}
    //--------------------TableModel InnerClass-------------
	@SuppressWarnings("serial")
	class MyTableModel extends AbstractTableModel {
        
		//table column headings
		private String[] columnNames = {"Author",
                                        "Title",
                                        "Number of Reviews",
                                        "Desicion Status",
                                        };
		//generates the data and displays it in the table
		private String[][] generateData() {
			final Set<Manuscript> the_mans = my_conf.getManuscripts();
			String[][] data = new String[the_mans.size()][4];
			int row = 0;
			for (Manuscript man : the_mans) {
				data[row][0] = man.getAuthor();
				data[row][1] = man.getTitle();
				data[row][2] = new Integer(man.getReviewsSize()).toString();
				data[row][3] = man.getStatus();
				row++;
			}
			
			return data;
		}
		
        protected Object[][] data = generateData();
        
        public int getColumnCount() {
            return columnNames.length;
        }
 
        public int getRowCount() {
            return data.length;
        }
 
        public String getColumnName(int col) {
            return columnNames[col];
        }
 
        public Object getValueAt(int row, int col) {
            return data[row][col];
        }
 
        //Makes only the change status cells editable
        public boolean isCellEditable(int row, int col) {
            if (col < 3) {
                return false;
            } else {
                return true;
            }
        }
 
        /*
         * Changes the data in a cell
         */
        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
        }
	}
}
